package cn.yoshii.ad.adserver.chang.rta

import cn.yoshii.ad.adserver.base.RequestUtil
import cn.yoshii.ad.adserver.param.DeviceRequest
import cn.yoshii.libs.jsonutil.toJsonString
import cn.yoshii.libs.okhttputil.OkhttpUtil
import cn.yoshii.libs.webstarter.util.MD5Util

/**
 * @author xiang
 * @Date 2024/2/15 21:42
 **/
object ChangRta {

    private const val TAG = "ChangRta"

    private const val CHANNEL_ID = "e7Dd6U"
    private const val CHANNEL_KEY = "41c1f06d70644df292db6fc99ccd8cbd"

    private const val URL = "http://rta.bohaiadx.com/rta"

    fun rta(request: DeviceRequest): List<RtaResult> {

        val time = System.currentTimeMillis()
        val token = MD5Util.md5("$CHANNEL_ID$CHANNEL_KEY$time")

        val rta = RtaRequest(
            request.imei,
            request.oaid,
            "",
            listOf(1, 2, 3, 4),
            time,
            CHANNEL_ID,
            token
        )

        return try {
            val resp = OkhttpUtil.postWithJson(URL, json = rta.toJsonString())
            val result = RequestUtil.formatResponse<RtaResponse>(
                resp, RtaResponse::class.java
            )
            result?.data?.results ?: listOf()
        } catch (e: Exception) {
            e.printStackTrace()
            listOf()
        }

    }

    fun fakeRta(): List<RtaResult>{
        return listOf(
            RtaResult(1, true),
            RtaResult(2, true),
            RtaResult(3, true),
            RtaResult(4, true),
        )
    }
}